package com.teach;

public class PrintABC implements Runnable {
	
	private char abc;
	private PrintABC next;
	
	private boolean waiting = false;
	

	public PrintABC(char abc) {
		super();
		this.abc = abc;
	}
	
	

	


	public boolean isWaiting() {
		return waiting;
	}






	public void setNext(PrintABC next) {
		this.next = next;
	}




	@Override
	public void run() {
		for(int i=0;i<10;i++) {
			synchronized (this) {
				try {
					waiting=true;
					this.wait();
				}catch (Exception e) {
				}
			}
			waiting = false;
			System.out.print(abc);
			synchronized (next) {
				next.notify();
			}
		}
	}

	public static void main(String[] args) {
		PrintABC a = new PrintABC('A');
		PrintABC b = new PrintABC('B');
		PrintABC c = new PrintABC('C');
		a.setNext(b);
		b.setNext(c);
		c.setNext(a);

		new Thread(a).start();
		new Thread(b).start();
		new Thread(c).start();
		
		//a,b,c 都进入等待后再唤醒a
		while(!a.isWaiting()||!b.isWaiting()||!c.isWaiting()) {
		}
		synchronized (a) {
			a.notify();
		}
	}
}
